package com.fgxia.driver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fgxia.driver.domain.bo.DriverInfoBo;
import com.fgxia.driver.domain.bo.DriverLicenseBo;
import com.fgxia.driver.domain.bo.DrivingLicenseBo;
import com.fgxia.driver.domain.bo.LoginDriverBo;
import com.fgxia.driver.domain.vo.DriverInfoVo;
import com.fgxia.driver.domain.vo.DriverLicenseVo;
import com.fgxia.driver.domain.vo.DrivingLicenseVo;
import com.fgxia.driver.domain.vo.LoginDriverVo;
import com.fgxia.driver.service.IDriverInfoService;
import com.ruoyi.business.domain.DriverDriversLicense;
import com.ruoyi.business.domain.DriverDrivingLicense;
import com.ruoyi.business.domain.SysUserLoginDriver;
import com.ruoyi.business.mapper.DriverDriversLicenseMapper;
import com.ruoyi.business.mapper.DriverDrivingLicenseMapper;
import com.ruoyi.business.mapper.SysUserLoginDriverMapper;
import com.ruoyi.common.utils.BeanCopyUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * driver info service
 *
 * @author yyt
 */
@Service
@RequiredArgsConstructor
public class DriverInfoService implements IDriverInfoService {

    private final SysUserLoginDriverMapper loginDriverMapper;

    private final DriverDriversLicenseMapper driverLicenseMapper;

    private final DriverDrivingLicenseMapper drivingLicenseMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean insertByBo(DriverInfoBo driverInfoBo) {
        long relevanceUserId = driverInfoBo.getRelevanceUserId();

        // todo 驾驶证、行驶证、身份证照片等校验
        // 保存用户
        LoginDriverBo loginDriverBo = driverInfoBo.getLoginDriverBo();

        LambdaQueryWrapper<SysUserLoginDriver> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysUserLoginDriver::getRelevanceUserId, relevanceUserId);
        SysUserLoginDriver sysUserLoginDriver = loginDriverMapper.selectOne(lqw);

        sysUserLoginDriver.setTel(loginDriverBo.getTel());
        sysUserLoginDriver.setRealName(loginDriverBo.getRealName());
        sysUserLoginDriver.setCardFrontUrl(loginDriverBo.getCardFrontUrl());
        sysUserLoginDriver.setCardBackUrl(loginDriverBo.getCardBackUrl());
        sysUserLoginDriver.setCarType(loginDriverBo.getCarType());
        sysUserLoginDriver.setCarLength(loginDriverBo.getCarLength());
        sysUserLoginDriver.setOpeningBank(loginDriverBo.getOpeningBank());
        sysUserLoginDriver.setCreditCardNumbers(loginDriverBo.getCreditCardNumbers());
        sysUserLoginDriver.setDriverType(loginDriverBo.getDriverType());
        sysUserLoginDriver.setUserImgUrl(loginDriverBo.getUserImgUrl());
        loginDriverMapper.updateById(sysUserLoginDriver);

        // 保存驾驶证
        DriverLicenseBo driverLicenseBo = driverInfoBo.getDriverLicenseBo();
        DriverDriversLicense driverLicense = BeanCopyUtils.copy(driverLicenseBo, DriverDriversLicense.class);
        driverLicense.setRelevanceUserId(relevanceUserId);
        driverLicenseMapper.insert(driverLicense);

        // 保存行驶证
        DrivingLicenseBo drivingLicenseBo = driverInfoBo.getDrivingLicenseBo();
        DriverDrivingLicense drivingLicense = BeanCopyUtils.copy(drivingLicenseBo, DriverDrivingLicense.class);
        drivingLicense.setRelevanceUserId(relevanceUserId);

        return drivingLicenseMapper.insert(drivingLicense) > 0;
    }

    @Override
    public DriverInfoVo selectByUserId(Long relevanceUserId) {
        DriverInfoVo driverInfoVo = new DriverInfoVo();

        LambdaQueryWrapper<SysUserLoginDriver> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysUserLoginDriver::getRelevanceUserId, relevanceUserId);
        SysUserLoginDriver sysUserLoginDriver = loginDriverMapper.selectOne(lqw);
        driverInfoVo.setLoginDriverVo(BeanCopyUtils.copy(sysUserLoginDriver, LoginDriverVo.class));

        LambdaQueryWrapper<DriverDriversLicense> driverLicenseLqw = new LambdaQueryWrapper<>();
        driverLicenseLqw.eq(DriverDriversLicense::getRelevanceUserId, relevanceUserId);
        DriverDriversLicense driverLicense = driverLicenseMapper.selectOne(driverLicenseLqw);
        driverInfoVo.setDriverLicenseVo(BeanCopyUtils.copy(driverLicense, DriverLicenseVo.class));

        LambdaQueryWrapper<DriverDrivingLicense> drivingLicenseLqw = new LambdaQueryWrapper<>();
        drivingLicenseLqw.eq(DriverDrivingLicense::getRelevanceUserId, relevanceUserId);
        DriverDrivingLicense drivingLicense = drivingLicenseMapper.selectOne(drivingLicenseLqw);
        driverInfoVo.setDrivingLicenseVo(BeanCopyUtils.copy(drivingLicense, DrivingLicenseVo.class));

        return driverInfoVo;
    }

    @Override
    public Boolean updateByBo(DriverInfoBo driverInfoBo) {
        long relevanceUserId = driverInfoBo.getRelevanceUserId();

        // todo 驾驶证、行驶证、身份证照片等校验
        // 保存用户
        LoginDriverBo loginDriverBo = driverInfoBo.getLoginDriverBo();

        LambdaQueryWrapper<SysUserLoginDriver> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysUserLoginDriver::getRelevanceUserId, relevanceUserId);
        SysUserLoginDriver sysUserLoginDriver = loginDriverMapper.selectOne(lqw);

        sysUserLoginDriver.setTel(loginDriverBo.getTel());
        sysUserLoginDriver.setRealName(loginDriverBo.getRealName());
        sysUserLoginDriver.setCardFrontUrl(loginDriverBo.getCardFrontUrl());
        sysUserLoginDriver.setCardBackUrl(loginDriverBo.getCardBackUrl());
        sysUserLoginDriver.setCarType(loginDriverBo.getCarType());
        sysUserLoginDriver.setCarLength(loginDriverBo.getCarLength());
        sysUserLoginDriver.setOpeningBank(loginDriverBo.getOpeningBank());
        sysUserLoginDriver.setCreditCardNumbers(loginDriverBo.getCreditCardNumbers());
        sysUserLoginDriver.setDriverType(loginDriverBo.getDriverType());
        sysUserLoginDriver.setUserImgUrl(loginDriverBo.getUserImgUrl());
        loginDriverMapper.updateById(sysUserLoginDriver);

        // 保存驾驶证
        DriverLicenseBo driverLicenseBo = driverInfoBo.getDriverLicenseBo();
        DriverDriversLicense driverLicense = BeanCopyUtils.copy(driverLicenseBo, DriverDriversLicense.class);
        driverLicense.setRelevanceUserId(relevanceUserId);
        driverLicenseMapper.updateById(driverLicense);

        // 保存行驶证
        DrivingLicenseBo drivingLicenseBo = driverInfoBo.getDrivingLicenseBo();
        DriverDrivingLicense drivingLicense = BeanCopyUtils.copy(drivingLicenseBo, DriverDrivingLicense.class);
        drivingLicense.setRelevanceUserId(relevanceUserId);

        return drivingLicenseMapper.updateById(drivingLicense) > 0;
    }
}
